package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.WareHousing;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.ISave;
import com.by.common.group.IUpdate;
import com.by.query.WareHousingQuery;
import com.by.service.WareHousingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/v1")
@Api(tags = "仓储管理")
public class WareHousingController extends BaseController {
    @Autowired
    WareHousingService wareHousingService;

    @ApiOperation("保存仓库")
    @PostMapping("/wareHousing")
    public ResultBean save(@Validated(ISave.class) @RequestBody WareHousing wareHousing, BindingResult result) {
        validate(result);
        int rs = wareHousingService.save(wareHousing);
        return saveResult(rs);
    }


    @ApiOperation("修改仓库")
    @PutMapping("/wareHousing")
    public ResultBean update(@Validated(IUpdate.class) @RequestBody WareHousing wareHousing, BindingResult result) {
        validate(result);
        int rs = wareHousingService.update(wareHousing);
        return updateResult(rs);
    }


    @ApiImplicitParam(name = "id", value = "仓库id", dataTypeClass = Integer.class, required = true, paramType = "query")
    @ApiOperation("删除仓库")
    @DeleteMapping("/wareHousing")
    public ResultBean delete(@RequestParam(name = "id", required = true, defaultValue = "0") int id) {
        int rs = wareHousingService.delete(id);

        return deleteResult(rs);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "仓库id", dataTypeClass = Integer.class, required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态0 禁用 1 启用", dataTypeClass = Integer.class, required = true, paramType = "query")
    })
    @ApiOperation("修改状态")
    @PutMapping("/wareHousing/status")
    public ResultBean updateStatus(@RequestParam(name = "id", required = true) int id, @RequestParam(name = "status", required = true, defaultValue = "0") int status) {
        int rs = wareHousingService.updateStatus(id, status);
        return updateResult(rs);
    }


    @ApiOperation("仓储列表")
    @PostMapping("/wareHousing/query")
    public ResultBean query(@RequestBody WareHousingQuery query){
        Map map= BeanUtil.beanToMap(query);
        PageBean bean=wareHousingService.query(map);
        return success("仓储列表",bean);
    }

    @ApiOperation("获取所有的仓储")
    @GetMapping("/wareHousing/queryList")
    public ResultBean getWareHousingList(){
        List<Map> list=wareHousingService.getWareHousingList();
        return success("仓储列表",list);
    }



}
